<template>
  <div class="register-admin">
    <ColoredCard title="账号添加说明" description="管理员账号继承开发者所有权限，并能够修改网站设置和查看产品" icon="el-icon-user" cardColor=#fcd303
      style="width 60%" />
    <hr style="border: 1px solid #ccc; margin: 20px 0;">
    <h2 style="padding-top: 40px; padding-left: 20px;" :style="{ color: theme }">添加管理员账号</h2>

    <el-form :model="formData" @submit.native.prevent="registerAdmin">
      <el-form-item label="用户名" :label-width="labelWidth">
        <el-input v-model="formData.username" placeholder="输入用户名" required prefix-icon="el-icon-user" />
      </el-form-item>

      <el-form-item label="密码" :label-width="labelWidth">
        <el-input type="password" v-model="formData.password" placeholder="输入密码" required prefix-icon="el-icon-lock" />
      </el-form-item>

      <el-form-item label="邮箱" :label-width="labelWidth">
        <el-input type="email" v-model="formData.email" placeholder="输入邮箱" required prefix-icon="el-icon-user" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="registerAdmin" :loading="isSubmitting" :disabled="isSubmitting">
          添加
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { register_admin } from "@/api/user"; // Import register_admin function
import ColoredCard from '@/components/ColoredCard/index.vue';
import { Message } from 'element-ui'


export default {
  computed: {
    theme() {
      return this.$store.state.settings.theme
    }
  },
  data() {
    return {
      formData: {
        username: '',
        password: '',
        email: ''
      },
      isSubmitting: false,
      labelWidth: '100px', // Set a label width for alignment
    };
  },
  components: {
    ColoredCard
  },
  methods: {
    async registerAdmin() {
      try {
        this.isSubmitting = true;
        // Call the register_admin function from the API module
        const response = await register_admin(this.formData);
        this.isSubmitting = false;

        Message({
          message: '注册成功!',
          type: 'success',
          duration: 5 * 1000
        })
      } catch (error) {
        this.isSubmitting = false;
      }
    }
  }
};
</script>

<style scoped>
.register-admin {
  width: 600px;
  margin: 50px auto;

  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.el-form {
  padding: 20px;
  margin-top: 20px;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-button {
  width: 100%;
  font-size: 16px;
}

.el-message {
  margin-top: 20px;
}

.clarification-card {
  margin-top: 30px;
}

.clarification-card h3 {
  font-size: 18px;
  margin-bottom: 10px;
}

.el-divider {
  margin: 10px 0;
}
</style>
